// pages/me/order/index.js
import util from '../../../utils/utils';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    initUser:true,
    typeMap:[
      {id:1,name:'全部订单'},
      {id:2,name:'有效订单'},
      {id:3,name:'失效订单'},
    ],
    listData:[],
    listPage: 1,
    listEmpty: false,
    listMore: true,
    ServiceText: false,
    type:1,
    status:1,
    currentOrder:'me'
  },
  initPage: function(){
    let self = this;
    wx.hideShareMenu();
    self.initData(true);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if(this.data.initUser !== true){
      return false;
    }
    this.initData(true);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let self = this;
    if (self.data.listMore === true) {
      let param = self.getApiParam(false);
      util.request({
        url:param.url,
        data:param,
        isJava:true,
        loading:true,
        callback:function (result) {
          let listData = self.data.listData;
          let _data  = [];
          let _total = 0;
          let more   = false;
          //处理统一的数据结构
          if(result['res']['orders']){
            _data  = self.formatData(result['res']['orders']);
            _total = result['res']['total'];
          }
          for (let i = 0; i < _data.length; i++) {
            listData.push(_data[i]);
          }
          if (listData.length < _total) {
            more = true;
          }
          self.setData({
            listData: listData,
            listPage:param.page,
            listMore:more
          });
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }
      });
    }
  },
  onChange(e) {
    this.setData({
      value: e.detail
    });
  },
  onSearch:function(event) {
    if(this.data.value === ''){
      util.msg('请输入要搜索的订单号!');
      return false;
    }
    if(this.data.value.length < 6){
      util.msg('最少需要输入订单号中的6位!');
      return false;
    }
    this.getApiData(true);
  },
  formatData: function (list) {
    let flag = this.data.currentOrder === 'team';
    for (let i = 0; i < list.length; i++) {
      list[i]['order_amount'] = util.formatMoney(list[i]['order_amount']);
      list[i]['commission'] = util.formatMoney(list[i]['commission']);
      list[i]['status_text'] = '已发货';
    }
    return list;
  },
  switchType:function(event){
    let old_type = this.data.type;
    let type = parseInt(event.detail.itemid);
    if(old_type !== type){
      this.setData({
        type:type,
      });
      this.initData(true);
    }
  },
  switchOrder:function(e){
    let old_type = this.data.currentOrder;
    let type     = e.currentTarget.dataset.type;
    if(old_type !== type){
      this.setData({
        type:1,
        currentOrder:type,
      });
      this.initData(true);
    }
  },
  /**
   * 处理查询参数
   * @param ref
   * @returns {{with_coupon: boolean, page_size: number}}
   */
  getApiParam:function(ref){
    let update = ref || false;
    let _data = {
      url:util.api.orderData,
      order_type:this.data.type - 1,
      type:this.data.type - 1,
      pagesize: 10,
    };
    if(!update){
      _data['page'] = this.data.listPage + 1;
    }else{
      _data['page'] = 1;
    }
    if(this.data.currentOrder === 'team'){
      _data['url'] = util.api.teamOrder
    }
    if(this.data.value !== '' && this.data.value.length >= 6){
      _data['order_sn'] = this.data.value;
    }
    return _data;
  },
  getApiData:function(ref){
    let update = ref || false;
    let self   = this;
    let param  = this.getApiParam(update);
    wx.showNavigationBarLoading();
    util.request({
      url:param.url,
      isJava:1,
      data:param,
      loading:1,
      callback:function (result) {
        let _data = [];
        let _total = 0;
        //处理统一的数据结构
        if(result['res']['orders']){
          _data  = self.formatData(result['res']['orders']);
          _total = result['res']['total'];
        }
        let more  = false;
        if (_data.length < _total) {
          more = true;
        }
        self.setData({
          listData: _data,
          listEmpty: _data <= 0,
          listPage:param.page,
          listMore:more
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  copyOrderSn:function(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.sn + ''
    });
  },
  initData: function (ref) {
    let self = this;
    let update = ref || false;
    self.getApiData(update);
  }
});
